package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import io.swagger.annotations.Api;
import com.microsoft.schemas.office.visio.x2012.main.CellType;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author spt
 * @description 预约设置控制器
 * @date
 **/
@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "后台预约设置接口")
public class OrderSettingController {
    @Autowired
    private  OrderSettingService orderSettingService;

    /**
     * controller：
     *      接受年、月数据
     *      调用查询方法
     *      返回OrderSetting数据
     * service：
     *      写接口  写方法
     *      写实现类 写方法
     *          调用 查询方法返回数据
     * mapper/dao：
     *      写dao
     *      mapper写sql
     *
     * @return
     */
    //查看预约人数
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String year,String month){
        log.info("getOrderSettingByMonth,year:"+year+"month:"+month);
    //调用查询方法
        List<Map> orderSetting = orderSettingService.getOrderSettingByMonth(year,month);
    //返回OrderSetting数据
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,orderSetting);
    }


    //单独设置预约人数
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }

    //批量添加设置
    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile) throws IOException, InvalidFormatException {
        List<OrderSetting> orderSettingList = new ArrayList<>();
        log.info("upload已进入");
        InputStream inputStream = excelFile.getInputStream();
        Workbook sheets = WorkbookFactory.create(inputStream);
        Sheet sheetAt = sheets.getSheetAt(0);//第0个工作页
        int lastRowNum = sheetAt.getLastRowNum();//最后一行
        for (int i = 1; i < lastRowNum; i++) {
            Row row = sheetAt.getRow(i);
//            short lastCellNum = row.getLastCellNum();//最后一列
            OrderSetting orderSetting = new OrderSetting();
            for (int j = 0; j < 2; j++) {
                Cell cell = row.getCell(j);
                if (j==0){
                    log.info("getDateCellValue"+cell.getDateCellValue());
                    orderSetting.setOrderDate(cell.getDateCellValue());
                }else{
                    log.info("getNumericCellValue()"+(int)cell.getNumericCellValue());
                    orderSetting.setNumber((int)cell.getNumericCellValue());
                }
            }
            orderSettingList.add(orderSetting);
        }
        sheets.close();
        orderSettingService.upload(orderSettingList);
        return new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }
}
